﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeepZoomPivotConstructor;
using DeepZoomPivotConstructor.VisualLib.Hotspots;
using DeepZoomPivotConstructor.Utilities;

namespace DeepZoomPivotConstructor.VisualLib.Utilities
{
    /// <summary>
    /// Class to help with wrapping up a sequence of visuals with hot spots to
    /// center and also to point to the next dude.
    /// </summary>
    public class VisualSeqenceWrapper : IEnumerable<Visual>
    {
        private IEnumerable<Visual> VisualsToWrap { get; set; }
        private Visual EndVisual;

        public VisualSeqenceWrapper(IEnumerable<Visual> stuffToWrap, Visual endPoint)
        {
            VisualsToWrap = stuffToWrap;
            EndVisual = endPoint;
        }

        /// <summary>
        /// Go through the sequence of images, getting the current guy as well
        /// as the next guy and linking them together.
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IEnumerator<Visual> GetEnumerator()
        {
            var indexer = VisualsToWrap.GetEnumerator();
            Visual lastItem = EndVisual;
            Visual currentItem = null;
            Visual nextItem = null;
            if (indexer.MoveNext())
            {
                currentItem = indexer.Current;
                if (indexer.MoveNext())
                {
                    nextItem = indexer.Current;
                }
            }
            while (currentItem != EndVisual && currentItem != null)
            {
                yield return WrapVisualInNavigationHS(lastItem, currentItem, nextItem, EndVisual);
                lastItem = currentItem;
                currentItem = nextItem;
                if (indexer.MoveNext())
                {
                    nextItem = indexer.Current;
                }
                else
                {
                    nextItem = EndVisual;
                }
            }
        }

        /// <summary>
        /// Given a visual, wrap it in a centering hot spot visual, also
        /// make sure that ew can "page" forward.
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        private Visual WrapVisualInNavigationHS(Visual vDotPrev, Visual v, Visual vDotNext, Visual vUp)
        {
            var bb = v.VisualBoundingBox;
            VisualHotSpot hs = new VisualHotSpot(bb.Width, bb.Height) {
                    new HSActionCenterHere(),
                    new HSActionCenterThere(vUp) {KeysToActivate = HotSpotActionBase.ActivationKeys.KeyUpArrow}
            };
            if (vDotPrev != null)
            {
                hs.Add(new HSActionCenterThere(vDotPrev) { KeysToActivate = HotSpotActionBase.ActivationKeys.KeyLeftArrow });
            }
            if (vDotNext != null)
            {
                hs.Add(new HSActionCenterThere(vDotNext) { KeysToActivate = HotSpotActionBase.ActivationKeys.KeyRightArrow });
            }
            hs.XPosition = bb.XPosition;
            hs.YPosition = bb.YPosition;
            hs.IncZIndex(v.ZIndex - 1);

            return v.WrapWithVisuals(new List<Visual>() {
                hs
            });
        }

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
